import { message } from 'antd';
import router from 'umi/router';
import {
  queryDictItemsByCodes,
  fetchList,
  deleteMany,
  switchRun,
  cityChange,
  handleSubmit,
  getContent,
  update,
} from '@/services/guideprotect';

export default {
  state: {
    formFieldsValue: {},
    activeKey: null,
    tabList: [],
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    },
    policyData: [],
    // 新增
    cityChild: [],
    // 详情
    affixArr: [],
    contentData: {},
    content: '',
    vno: '',
    // 修改
    detailId: '',
  },

  effects: {
    *update({ payload }, { call }) {
      const res = yield call(update, payload);
      if (res.success) {
        router.push('/searchmanage/guideprotect/index');
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *getContent({ payload }, { call, put }) {
      const res = yield call(getContent, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            contentData: res.data,
            affixArr: res.data.affix ? res.data.affix.split(',') : [],
            content: res.data.content,
            vno: res.data.vno,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res;
    },
    *handleSubmit({ payload }, { call }) {
      const res = yield call(handleSubmit, payload);
      return res;
    },
    *cityChange({ payload }, { call, put }) {
      const res = yield call(cityChange, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            cityChild: res.data ? res.data : [],
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *switchRun({ payload }, { call }) {
      const res = yield call(switchRun, payload);
      return res;
    },
    *deleteMany({ payload }, { call }) {
      const res = yield call(deleteMany, payload);
      return res;
    },
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(fetchList, payload); // 请求
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList', // 处理后台返回数据
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *queryDictItemsByCodes({ payload }, { call, put }) {
      const res = yield call(queryDictItemsByCodes, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: {
            policyData: res.data.BIZ_INS_TYPE.concat(res.data.BIZ_ACCU_TYPE),
          },
        });
      }
    },
  },

  reducers: {
    // 设置页签状态
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: {
            total: data.totalCount,
            current: currentPage,
            pageSize,
          },
        },
      };
    },
  },
};
